//
//  PickupController.m
//  iphone-graph
//
//  Created by yuzhuo on 2017/1/3.
//  Copyright © 2017年 yzapp. All rights reserved.
//

#import "PickupController.h"
#import "Constant.h"
#import "PUCollectionViewCell.h"
#import "ImageViewController.h"

@interface PickupController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView * collectView;

/** data */
@property (nonatomic,strong) NSMutableArray<UIImage *> *data;
@end

@implementation PickupController

-(instancetype) init
{
    self = [super init];
    if(self){
        //1.初始化layout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //设置collectionView滚动方向
        //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        //设置headerView的尺寸大小
//        layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);
        //该方法也可以设置itemSize
//        layout.itemSize =CGSizeMake(110, 150);
        
        //2.初始化collectionView
        self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH-30, SCREEN_HEIGHT) collectionViewLayout:layout];
        
        //3.注册collectionViewCell
        //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
        [self.collectView registerClass:[PUCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];

        
        //4.设置代理
        self.collectView.delegate = self;
        self.collectView.dataSource = self;
        [self initData];
    }
    return self;
}

-(void) initData{
    
    _data = [NSMutableArray new];
    
    for(int i =0;i<9;i++){
        UIImage *image = [UIImage imageNamed:@"bgVote"];
        [_data addObject:image];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = self.titleName;
    [self.view addSubview:self.collectView];
    self.collectView.backgroundColor = [UIColor clearColor];

    // Do any additional setup after loading the view.
}

#pragma mark - CollectionViewDataSourceDelegate

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.data.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    PUCollectionViewCell *cell = (PUCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    
    cell.data = self.data[indexPath.row];
    
    return cell;
}


#pragma mark -- UICollectionViewDelegate
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREEN_WIDTH-50)/3, (SCREEN_WIDTH-50)/3);
}

//设置每个item的UIEdgeInsets
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(10, 10, 10, 10);
//}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageViewController *detailview = [ImageViewController new];
    [self.navigationController pushViewController:detailview animated:YES];

    NSLog(@"%ld",indexPath.row);
}
@end
